// 引入mongoose
const mongoose = require("mongoose");
// 引入放映厅模型
// const { projectionHallModel } = require("../db/model/projectionHall")

// 获取服务端传到数据库的数据{curPage,eachPage}
const get = async ({ curPage, eachPage }) => {
    const projectionHallModel = mongoose.model("projectionHall")
    curPage = ~~curPage;
    eachPage = ~~eachPage;
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    }
    // 通过model方法获取模型 一个参数获取 三个参数创建
    // const projectionHallModel = mongoose.model("projectionHall");
    // 获取当前model映射的集合里面存在的文档总数
    data.total = await projectionHallModel.countDocuments();
    data.maxPage = Math.ceil(data.total / eachPage);
    // 跳过数据条数
    const skipNum = (curPage - 1) * eachPage;
    // 查询所有数据，并跳过skipnum条，查eachpage条并执行
    data.rows = await projectionHallModel.find().skip(skipNum).limit(eachPage).exec();
    return data;
}

// 添加放映厅
const add = async ({ name, status, cinemasId }) => {
    const projectionHallModel = mongoose.model("projectionHall");
    const seatsModel=mongoose.model("seats");
    // 添加文档到模型
    const data = await projectionHallModel.create({ name, status, cinemasId });
    const seatArr = [];
    for (let i = 1; i <= 9; i++) {
        for (let j = 1; j <= 9; j++) {
            seatArr.push({
                name: `${i}排${j}列`,
                projectionHallId: data,
            })
        }
    }
    await seatsModel.create(seatArr);
    return data;
}

// 删除放映厅
const del = async (_id) => {
    const projectionHallModel = mongoose.model("projectionHall")
    const seatsModel=mongoose.model("seats")
    const data = { status: true };
    // 删除该放映厅对应的座位
    await seatsModel.remove(_id);
    // 删除该放映厅
    await projectionHallModel.remove(_id);
    // 返回状态值
    return data;
}

// 修改放映厅信息
const reset = async ({ Name, status, cinemasId, _id }) => {
    const projectionHallModel = mongoose.model("projectionHall")
    // 更新修改信息
    await projectionHallModel.updateOne({ _id }, { name: Name, status: status })
    const data = { status: true };
    return data;
}

// 获取一个放映厅的数据
const getOne = async _id => {
    const projectionHallModel = mongoose.model("projectionHall")
    // 添加文档到模型
    return await projectionHallModel.findOne({ _id });
}

// 获取电影院对应放映厅的数据
const getTheater = async cinemasId => {
    const projectionHallModel = mongoose.model("projectionHall")
    // 添加文档到模型
    return await projectionHallModel.find({ cinemasId });
}

// 获取电影院对应放映厅的数据
const find = async ({ cinemaId, status, _id }) => {
    // 引入放映厅模型
    const projectionHallModel = mongoose.model("projectionHall")
    // 判断哪个值没有选中
    if (!status) {
        // console.log(status !== true);
        console.log(status);
        console.log("in");
        if (!_id) {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ cinemasId: cinemaId });
        } else if (!cinemaId) {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ _id: _id });
        } else {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ cinemasId: cinemaId, _id: _id });
        }
    } else if (!_id) {
        console.log("id");
        if (!status) {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ cinemasId: cinemaId });
        } else if (!cinemaId) {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ status });
        } else if (status) {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ cinemasId: cinemaId, status });
        }
    } else if (cinemaId) {
        if (!status) {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ _id: _id });
        } else if (!_id) {
            console.log("status");
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ status });
        } else if (status) {
            // 调用model身上find方法查询并返回查询数据
            return await projectionHallModel.find({ _id: _id, status });
        }
    }
    // 添加文档到模型
    return await projectionHallModel.find({ cinemaId, status, _id });
}

// 暴露方法接口
module.exports = { get, add, del, reset, getOne, getTheater, find }